package it.unibz.easytravel.travel;

import it.unibz.easytravel.R;
import it.unibz.easytravel.blogging.BloggingActivity;
import it.unibz.easytravel.db.TravelDbAdapter;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainApplicationActivity extends Activity {
	private static Activity activity;
	
	private static final String TAG = "MAIN WINDOW";
	public static final String PREFS_FILE_NAME = "EasyTravelPrefsFile";
	public static final String PREF_NAME = "current_travel_id";
	public TextView travel_title_textView;
	private String travel_title;
	private ImageButton button_expenses;
	private ImageButton button_settings;
	private ImageButton button_blogger;
	private ImageButton button_planning;
	private ImageButton button_create_travel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.activity = this;	
		
		
		button_expenses = (ImageButton)findViewById(R.id.Button_expenses);
		button_expenses.setOnClickListener(mExpenses);
		
		button_settings = (ImageButton)findViewById(R.id.Button_settings);
		button_settings.setOnClickListener(mSettings);
		
		button_blogger = (ImageButton)findViewById(R.id.Button_blogger);
		button_blogger.setOnClickListener(mBlogger);
		
		button_planning = (ImageButton)findViewById(R.id.Button_planning);
		button_planning.setOnClickListener(mPlanning);
		
		button_create_travel = (ImageButton)findViewById(R.id.Button_create_travel);
		button_create_travel.setOnClickListener(mCreateTravel);
		
		travel_title_textView = (TextView) findViewById(R.id.travel_title);
		
		refreshView();
		
	}
	
	private void refreshView() {
		travel_title = getTravelName ();
		if (travel_title != null)	{
			button_create_travel.setVisibility(Button.INVISIBLE);
			button_expenses.setVisibility(Button.VISIBLE);
			button_settings.setVisibility(Button.VISIBLE);
			button_blogger.setVisibility(Button.VISIBLE);
			button_planning.setVisibility(Button.VISIBLE);			
			travel_title_textView.setText(getTravelName());
		}
		else {
			button_create_travel.setVisibility(Button.VISIBLE);
			button_expenses.setVisibility(Button.INVISIBLE);
			button_settings.setVisibility(Button.INVISIBLE);
			button_blogger.setVisibility(Button.INVISIBLE);
			button_planning.setVisibility(Button.INVISIBLE);				
		}
		travel_title_textView.setText(travel_title);
	}
	
	private String getTravelName() {
		String name = null;
		SharedPreferences prefs = getSharedPreferences(PREFS_FILE_NAME, 0); 
        int restoredTravelId = prefs.getInt(PREF_NAME, -1);        
        if (restoredTravelId == -1)
			Log.d(TAG, "The travel is not set");
		else {
			Log.d(TAG, "Travel is set " + restoredTravelId);
			try {

				TravelDbAdapter mTravelAdapter = new TravelDbAdapter(activity);
				mTravelAdapter.open();
				Cursor cursor = mTravelAdapter.getTravels(restoredTravelId);
				cursor.moveToFirst();
				name = cursor.getString(cursor
						.getColumnIndexOrThrow(TravelDbAdapter.TITLE));
				mTravelAdapter.close();
				Log.d(TAG, "Travel name " + name);				
				

			} catch (Exception e) {
				Log.d(TAG, "Something bad while taking the name of the travel");
			}
		}
        return name;
	}
	
	protected void onResume() {
		super.onResume();
		refreshView();
	}
	
	private OnClickListener mCreateTravel = new OnClickListener()
	{
		public void onClick(View v)
		{
			Intent intent = new Intent(activity, AddTravel.class);
			startActivity(intent);
		}
	};
	
	private OnClickListener mExpenses = new OnClickListener()
	{
		public void onClick(View v)
		{
			Intent intent = new Intent(MainApplicationActivity.this, ExpensesActivity.class);
            startActivity(intent);
		}
	};
	
	private OnClickListener mSettings = new OnClickListener()
	{
		public void onClick(View v)
		{
			Intent intent = new Intent(MainApplicationActivity.this, TravelSettingsActivity.class);
            startActivity(intent);
		}
	};
	private OnClickListener mBlogger = new OnClickListener()
	{
		public void onClick(View v)
		{
			Intent intent = new Intent(MainApplicationActivity.this, BloggingActivity.class);
            startActivity(intent);
			//Toast.makeText(MainApplicationActivity.this, "Not implemented yet!", Toast.LENGTH_SHORT).show();
		}
	};
	private OnClickListener mPlanning = new OnClickListener()
	{
		public void onClick(View v)
		{
			Intent intent = new Intent(MainApplicationActivity.this, PlanningActivity.class);
            startActivity(intent);
			//Toast.makeText(MainApplicationActivity.this, "Not implemented yet!", Toast.LENGTH_SHORT).show();
		}
	};
	

}
